package com.nuo.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nuo.dao.ConnectionManager;
import com.nuo.util.ValidateUtil;

public class LaunchServlet extends HttpServlet{
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.myService(req, resp);
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.myService(req, resp);
	}
	
	private void myService(HttpServletRequest req, HttpServletResponse rep)
			throws ServletException, IOException {
		String dbtype = req.getParameter("dbtype");
		String url = req.getParameter("url");
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		if(password == null){
			password = "";
		}
		
		if(ValidateUtil.isBlack(dbtype) || 
				ValidateUtil.isBlack(url)|| 
				ValidateUtil.isBlack(username)){
			req.getRequestDispatcher("index.jsp").forward(req, rep);
		}else{
			ConnectionManager.init(dbtype, url, username, password);
			req.getRequestDispatcher("error.jsp").forward(req, rep);
		}
		
		
	}
}
